package com.ws.shop.util;

import java.io.File;
import java.io.FileOutputStream;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.ws.shop.bean.SystemConfig;
/**
 * 工具类 - 系统配置
 */

public class SystemConfigUtil{
	
	public static final String CONFIG_FILE_NAME = "/WEB-INF/config/shop.xml";// 系统配置文件名称
	public static final String SYSTEM_CONFIG_CACHE_KEY = "systemConfig";// systemConfig缓存Key
	public static final String SYSTEM_SYSTEMCONFIG_NODE = "/systemConfig/";
	public static final long order_expired = 0;
	public static final long rmb_beat = 0;
	
	//存放必要的系统配置信息
	private static SystemConfig systemConfig;
	
	static{
		getSystemConfig();
	}
	
	/**
	 * 获取系统配置信息
	 * 
	 * @return SystemConfig对象
	 */
	public static SystemConfig getSystemConfig() {
		if (systemConfig != null) {
			return systemConfig;
		}
		File configFile = null;
		Document document = null;
		try {
			String configFilePath = SpringUtil.getRealPath(CONFIG_FILE_NAME);
			configFile = new File(configFilePath);
			SAXReader saxReader = new SAXReader();
			document = saxReader.read(configFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Node services_tel = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"services_tel");
		Node website = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"website");
		Node services_email = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"services_email");
		Node copyright = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"copyright");
		Node webname = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"webname");
		Node beat_name = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"beat_name");
		Node keywords = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"keywords");
		Node description = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"description");
		Node work_time = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"work_time");
		Node icp = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"icp");
		Node sina_blog = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"sina_blog");
		Node pointb_name = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"pointb_name");
		Node sms_Interface = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"sms_Interface");
		Node sms_success = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"sms_success");
		Node gift_pointb = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"gift_pointb");
		Node punch_pointb = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"punch_pointb");
		Node rmb_beat = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"rmb_beat");
		Node memcache_prefix = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"memcache_prefix");
		Node memcache_server = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"memcache_server");
		Node memcache_pconnect = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"memcache_pconnect");
		Node memcache_port = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"memcache_port");
		Node eaccelerator = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"eaccelerator");
		Node xcache = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"xcache");
		Node invite = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"invite");
		Node pointc_name = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"pointc_name");
		Node order_expired = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"order_expired");
		Node upload_head = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"upload_head");
		Node mauction_time = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"mauction_time");
		Node eauction_time = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"eauction_time");
		Node letter_replacestr = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"letter_replacestr");
		Node robots_probability = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"robots_probability");
		Node bottom_navigation_categories = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"bottom_navigation_categories");
		systemConfig = new SystemConfig();
		systemConfig.setServices_tel(services_tel.getText());
		systemConfig.setWebsite(website.getText());
		systemConfig.setServices_email(services_email.getText());
		systemConfig.setCopyright(copyright.getText());
		systemConfig.setWebname(webname.getText());
		systemConfig.setBeat_name(beat_name.getText());
		systemConfig.setKeywords(keywords.getText());
		systemConfig.setDescription(description.getText());
		systemConfig.setWork_time(work_time.getText());
		systemConfig.setIcp(icp.getText());
		systemConfig.setSina_blog(sina_blog.getText());
		systemConfig.setPointb_name(pointb_name.getText());
		systemConfig.setSms_Interface(sms_Interface.getText());
		systemConfig.setSms_success(sms_success.getText());
		systemConfig.setGift_pointb(gift_pointb.getText());
		systemConfig.setPunch_pointb(punch_pointb.getText());
		systemConfig.setRmb_beat(rmb_beat.getText());
		systemConfig.setMemcache_prefix(memcache_prefix.getText());
		systemConfig.setMemcache_server(memcache_server.getText());
		systemConfig.setMemcache_pconnect(memcache_pconnect.getText());
		systemConfig.setMemcache_port(Integer.parseInt(memcache_port.getText()));
		systemConfig.setEaccelerator(Integer.parseInt(eaccelerator.getText()));
		systemConfig.setXcache(Integer.parseInt(xcache.getText()));
		systemConfig.setInvite(Integer.parseInt(invite.getText()));
		systemConfig.setPointc_name(pointc_name.getText());
		systemConfig.setOrder_expired(Integer.parseInt(order_expired.getText()));
		systemConfig.setUpload_head(Integer.parseInt(upload_head.getText()));
		systemConfig.setMauction_time(Integer.parseInt(mauction_time.getText()));
		systemConfig.setEauction_time(Integer.parseInt(eauction_time.getText()));
		systemConfig.setLetter_replacestr(letter_replacestr.getText());
		systemConfig.setRobots_probability(Integer.parseInt(robots_probability.getText()));
		systemConfig.setBottomNavigationCategories(bottom_navigation_categories.getText());
		return systemConfig;
	}
	
	/**
	 * 更新系统配置信息
	 * 
	 * @param systemConfig
	 *          SystemConfig对象
	 */
	public static void update(SystemConfig systemConfig) {
		File configFile = null;
		Document document = null;
		try {
			String configFilePath = SpringUtil.getRealPath(CONFIG_FILE_NAME);
			configFile = new File(configFilePath);
			SAXReader saxReader = new SAXReader();
			document = saxReader.read(configFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Element rootElement = document.getRootElement();
		Node services_tel = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"services_tel");
		Node website = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"website");
		Node services_email = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"services_email");
		Node copyright = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"copyright");
		Node webname = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"webname");
		Node beat_name = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"beat_name");
		Node keywords = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"keywords");
		Node description = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"description");
		Node work_time = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"work_time");
		Node icp = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"icp");
		Node sina_blog = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"sina_blog");
		Node pointb_name = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"pointb_name");
		Node sms_Interface = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"sms_Interface");
		Node sms_success = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"sms_success");
		Node gift_pointb = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"gift_pointb");
		Node punch_pointb = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"punch_pointb");
		Node rmb_beat = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"rmb_beat");
		Node memcache_prefix = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"memcache_prefix");
		Node memcache_server = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"memcache_server");
		Node memcache_pconnect = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"memcache_pconnect");
		Node memcache_port = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"memcache_port");
		Node eaccelerator = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"eaccelerator");
		Node xcache = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"xcache");
		Node invite = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"invite");
		Node pointc_name = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"pointc_name");
		Node order_expired = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"order_expired");
		Node upload_head = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"upload_head");
		Node mauction_time = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"mauction_time");
		Node eauction_time = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"eauction_time");
		Node letter_replacestr = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"letter_replacestr");
		Node robots_probability = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"robots_probability");
		Node bottom_navigation_categories = document.selectSingleNode(SYSTEM_SYSTEMCONFIG_NODE+"bottom_navigation_categories");
		if(services_tel == null) services_tel = rootElement.addElement("services_tel");
		if(website == null) website = rootElement.addElement("website");
		if(services_email == null) services_email = rootElement.addElement("services_email");
		if(copyright == null) copyright = rootElement.addElement("copyright");
		if(webname == null) webname = rootElement.addElement("webname");
		if(beat_name == null) beat_name = rootElement.addElement("beat_name");
		if(keywords == null) keywords = rootElement.addElement("keywords");
		if(description == null) description = rootElement.addElement("description");
		if(work_time == null) work_time = rootElement.addElement("work_time");
		if(icp == null) icp = rootElement.addElement("icp");
		if(sina_blog == null) sina_blog = rootElement.addElement("sina_blog");
		if(pointb_name == null) pointb_name = rootElement.addElement("pointb_name");
		if(sms_Interface == null) sms_Interface = rootElement.addElement("sms_Interface");
		if(sms_success == null) sms_success = rootElement.addElement("sms_success");
		if(gift_pointb == null) gift_pointb = rootElement.addElement("gift_pointb");
		if(punch_pointb == null) punch_pointb = rootElement.addElement("punch_pointb");
		if(rmb_beat == null) rmb_beat = rootElement.addElement("rmb_beat");
		if(memcache_prefix == null) memcache_prefix = rootElement.addElement("memcache_prefix");
		if(memcache_server == null) memcache_server = rootElement.addElement("memcache_server");
		if(memcache_pconnect == null) memcache_pconnect = rootElement.addElement("memcache_pconnect");
		if(memcache_port == null) memcache_port = rootElement.addElement("memcache_port");
		if(eaccelerator == null) eaccelerator = rootElement.addElement("eaccelerator");
		if(xcache == null) xcache = rootElement.addElement("xcache");
		if(invite == null) invite = rootElement.addElement("invite");
		if(pointc_name == null) pointc_name = rootElement.addElement("pointc_name");
		if(order_expired == null) order_expired = rootElement.addElement("order_expired");
		if(upload_head == null) upload_head = rootElement.addElement("upload_head");
		if(mauction_time == null) mauction_time = rootElement.addElement("mauction_time");
		if(eauction_time == null) eauction_time = rootElement.addElement("eauction_time");
		if(letter_replacestr == null) letter_replacestr = rootElement.addElement("letter_replacestr");
		if(robots_probability == null) robots_probability = rootElement.addElement("robots_probability");
		if(bottom_navigation_categories == null) bottom_navigation_categories= rootElement.addElement("bottom_navigation_categories");
		services_tel.setText(systemConfig.getServices_tel());
		website.setText(systemConfig.getWebsite());
		services_email.setText(systemConfig.getServices_email());
		copyright.setText(String.valueOf(systemConfig.getCopyright()));
		webname.setText(String.valueOf(systemConfig.getWebname()));
		beat_name.setText(String.valueOf(systemConfig.getBeat_name()));
		keywords.setText(String.valueOf(systemConfig.getKeywords()));
		description.setText(String.valueOf(systemConfig.getDescription()));
		work_time.setText(String.valueOf(systemConfig.getWork_time()));
		icp.setText(String.valueOf(systemConfig.getIcp()));
		sina_blog.setText(String.valueOf(systemConfig.getSina_blog()));
		pointb_name.setText(String.valueOf(systemConfig.getPointb_name()));
		sms_Interface.setText(String.valueOf(systemConfig.getSms_Interface()));
		sms_success.setText(String.valueOf(systemConfig.getSms_success()));
		gift_pointb.setText(String.valueOf(systemConfig.getGift_pointb()));
		punch_pointb.setText(String.valueOf(systemConfig.getPunch_pointb()));
		rmb_beat.setText(String.valueOf(systemConfig.getRmb_beat()));
		memcache_prefix.setText(String.valueOf(systemConfig.getMemcache_prefix()));
		memcache_server.setText(String.valueOf(systemConfig.getMemcache_server()));
		memcache_pconnect.setText(String.valueOf(systemConfig.getMemcache_pconnect()));
		memcache_port.setText(String.valueOf(systemConfig.getMemcache_port()));
		eaccelerator.setText(String.valueOf(systemConfig.getEaccelerator()));
		xcache.setText(String.valueOf(systemConfig.getXcache()));
		invite.setText(String.valueOf(systemConfig.getInvite()));
		pointc_name.setText(String.valueOf(systemConfig.getPointc_name()));
		order_expired.setText(String.valueOf(systemConfig.getOrder_expired()));
		upload_head.setText(String.valueOf(systemConfig.getUpload_head()));
		mauction_time.setText(String.valueOf(systemConfig.getMauction_time()));
		eauction_time.setText(String.valueOf(systemConfig.getEauction_time()));
		letter_replacestr.setText(String.valueOf(systemConfig.getLetter_replacestr()));
		robots_probability.setText(String.valueOf(systemConfig.getRobots_probability()));
		bottom_navigation_categories.setText(String.valueOf(systemConfig.getBottomNavigationCategories()));
		try {
			OutputFormat outputFormat = OutputFormat.createPrettyPrint();// 设置XML文档输出格式
			outputFormat.setEncoding("UTF-8");// 设置XML文档的编码类型
			outputFormat.setIndent(true);// 设置是否缩进
			outputFormat.setIndent("	");// 以TAB方式实现缩进
			outputFormat.setNewlines(true);// 设置是否换行
			XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(configFile), outputFormat);
			xmlWriter.write(document);
			xmlWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		SystemConfigUtil.systemConfig = systemConfig;
	}
	
}